<?php

namespace App\Models\Goods;

use Illuminate\Database\Eloquent\Model;

class GoodsStore extends Model
{

    protected $table = 'wms_goodsstore';

    protected $primaryKey = 'storeID';

    public $timestamps = false;

    public $incrementing = false;

    /**
     * 查询商品库存
     * @param string $product_code 商品编号
     * @param int $store_spec 库存规格
     * @param int $storeID  仓库id
     * @return int|null
     */
    public static function search($product_code, $store_spec, $storeID = 1 )
    {

        if ( !$product_code || !$store_spec ) {
            return 0;
        }

        $store = self::where(['storeID' => $storeID, 'product_code' => $product_code ])->first();
        if (!$store || $store->enableSaleAmount == 0) {
            return 0;
        }

        $enable_sale_amount = floor($store->enableSaleAmount / $store_spec);

        return $enable_sale_amount;

    }


}
